var register = new Object();
var login = new Object();
var site_url = $("#site_url").val() ;

//function displayCode(){
//	$("#img_code").attr("style","display:''");
//	document.getElementById('img_code').src="code_show.html&"+Math.random();
//	$("#a_click").attr("style","display:''");
//}
//
//$("#code").focus(function(e){
//	displayCode();
//	return;
//})
//
//$("#code").change(function(e){
//	var code = $(this).attr("value");
//	if(code!=""){
//			$("#span_error_code").css('display','none');
//			$("#span_error_code").html('');
//	}else{
//		$("#span_error_code").css('display','inline-block');
//		$("#span_error_code").html('验证码错误');		
//		return;
//	}
//})
//
//function hcode(){
//	document.getElementById('code').src="code_show.html&"+Math.random();
//}


var email_check = false;
$("#email").change(function(){
	//var emailRegExp = new RegExp("[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?");
	var email = $(this).attr("value");
	if($.trim(email)!=""){
		if(!email.match(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/)){
			alert('Email格式不正确,请重新输入');
			return;
		}else{
			email_check = true;
		}
	}else{
		alert('Email不能为空,请重新输入');		
		return ;
	}
})


$("#password").change(function(){
	//var emailRegExp = new RegExp("[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?");
	var password = $(this).attr("value");
	if($.trim(password)==""){
		alert('密码不能为空,请重新输入');		
	}
})


$("#agreement").click(function(){
	//var emailRegExp = new RegExp("[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?");
	var agreement = $(this).attr("checked");
	if(agreement!="checked"){
		alert('请您同意100课堂服务使用协议');		
	}
})

$("#password1").change(function(){
	//var emailRegExp = new RegExp("[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?");
	var password1 = $(this).attr("value");
	if(span_error_password!=""){
			$("#span_error_password1").css('display','none');
			$("#span_error_password1").html('');
	}else{
		$("#span_error_password1").css('display','inline-block');
		$("#span_error_password1").html('确认密码不能为空,请重新输入');		
	}
})

//var phone_check = false;
$("#phone").change(function(){
	var phone = $(this).attr("value");
	if($.trim(phone)!=""){
		if(!phone.match(/^1[3|4|5|8][0-9]\d{4,8}$/)){
			alert('手机格式不正确,请重新输入');
			return;
		}
	}
})

var register_user = false;
var login_user = false;
$("#register_submit").click(function(){
	var email = $("#email").attr("value");
	var id2=$(this).attr("id2");
	if(email!=""){
		if(!email.match(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/)){
			alert('Email格式不正确,请重新输入');
		}else{
			email_check = true;
		}
	}else{
		alert('Email不能为空,请重新输入');	
		return;
	}
	var phone = $("#phone").attr("value");
	if($.trim(phone)!=""){
		if(!phone.match(/^1[3|4|5|8][0-9]\d{4,8}$/)){
			alert('手机格式不正确,请重新输入');
			return;
		}
	}
	
	var password = $("#password").attr("value");
	if($.trim(password)==''){
		alert('密码不能为空,请重新输入');	
		return;
	}
	
	var agreement = $("#agreement").attr("checked");
	if(agreement!='checked'){
		alert('请您同意100课堂服务使用协议');	
		return;
	}
	var agreement_flg =  $("#agreement").attr("value");
	var english_name = $("english_name").attr("value");
	if(email_check==true){
		var postJsondata ={"email":email,"phone":phone,"agreement":agreement_flg,"password":password,"english_name":english_name};
		$.ajax({
			 type:'POST',
			 url:site_url+'index_subRegister.html',
			 data:postJsondata,
			 dataType: 'json',
			 success:register.response,
			 async:false
			});
	}
	
	if(register_user==true){
		var postJsondata ={"email":email,"agreement":agreement_flg,"register_user":1,"password":password};
		$.ajax({
			 type:'POST',
			 url:site_url+'index_subLogin.html',
			 data:postJsondata,
			 dataType: 'json',
			 success:login.response,
			 async:false
			});	
		
		if(id2=="1"){
			if(login_user==true){
				location.href=site_url+"user_home.htm";
			}else{
				alert('登录失败,请重新登录');
			}
		}
		if(id2=="2"){
			if(login_user==true){
				location.href=site_url+"user_home.htm";
			}else{
				alert('登录失败,请重新登录');
			}
		}
	}
})
register.response = function(res){
	if(res.success=="fail"){
		if(res.res_code==4){
			alert('邮箱已被注册,请更换邮箱');
			register_user = false;
			return;
		}
		if(res.res_code==5){
			alert('手机号码已被注册,请更换手机');
			register_user = false;
			return;
		}
	}
	
	if(res.success=="success"){
		register_user = true;
	}
}


login.response = function(res){
	if(res.res_code==true){
		login_user = true;
		return;
	}
	if(res.res_code==false){
		alert('用户名或者密码错误,请重新输入');		
		login_user = false;
		return;
	}
}

$("#login_email").change(function(){
	var email = $(this).attr("value");
	if(email!=""){
		email_check = true;
	}else{
		alert('Email不能为空,请重新输入');		
		return ;
	}
})

$("#login_email2").change(function(){
	var email = $(this).attr("value");
	if(email!=""){
		email_check = true;
	}else{
		alert('Email不能为空,请重新输入');		
		return ;
	}
})

$("#login_password").change(function(){
	var password = $(this).attr("value");
	if(password!=""){
	}else{
		alert('密码不能为空,请重新输入');		
	}
})

$("#login_password2").change(function(){
	var password = $(this).attr("value");
	if(password!=""){
	}else{
		alert('密码不能为空,请重新输入');		
	}
})

$("#login_agreement").click(function(){
	var agreement = $(this).attr("checked");
	if(agreement=="checked"){
	}else{
		alert('请您同意100课堂服务使用协议');		
	}
})

/*
 * 登陆 ajax 请求
 */
var loginFn = function (email, password) {
	var postJsondata ={"email":email,"password":password};
	$.ajax({
		 type:'POST',
		 url:site_url+'index_subLogin.html',
		 data:postJsondata,
		 dataType: 'json',
		 success:login.response,
		 async:false
	 });		
	if(id2 == '1'){
		if(login_user==true){
			location.href=site_url+"user_home.htm";
		}
	}
	
	if(id2 == '2'){
		if(login_user==true){
			alert('注册成功,系统将转入登陆页面')
			location.href=site_url+"user_home.htm";
		}
	}
}
$("#login_submit").click(function(){
	var email = $("#login_email").attr("value");
	var id2=$(this).attr("id2");	
	if(email==""){
		alert('Email不能为空,请重新输入');
		return;
	}
	var password = $("#login_password").attr("value");
	if(password==""){
		alert('密码不能为空,请重新输入');
		return;
	}

	loginFn(email, password);
})

$("#login_submit2").click(function(){
	var email = $("#login_email2").attr("value");
	var id2=$(this).attr("id2");	
	if(email==""){
		alert('Email不能为空,请重新输入');
		return;
	}
	var password = $("#login_password2").attr("value");
	if(password==""){
		alert('密码不能为空,请重新输入');
		return;
	}
	loginFn(email, password);
});


